package com.maibow.util;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.util.Date;
import java.util.List;

public class PrinterDao implements Printable {
    private List<PrinterMessage> list;

    public PrinterDao(List<PrinterMessage> list) {
        this.list = list;
    }

    @Override
    public int print(Graphics gra, PageFormat pf, int pageIndex) throws PrinterException {
        System.out.println(list);
        try {
            /**
             * * @param Graphic指明打印的图形环境 * @param
             * PageFormat指明打印页格式（页面大小以点为计量单位，1
             * 点为1英寸的1/72，1英寸为25.4毫米。A4纸大致为595×842
             * 点） * @param pageIndex指明页号
             **/
            // 转换成Graphics2D
            Graphics2D g2_1 = (Graphics2D) gra;
            // Graphics2D g2_2 = (Graphics2D) gra;
            // 设置打印颜色为黑色
            g2_1.setColor(Color.black);
            // g2_2.setColor(Color.black);
            // 打印起点坐标
            switch (pageIndex) {
                case 0:
                    // 设置打印字体（字体名称、样式和点大小）（字体名称可以是物理或者逻辑名称）
                    // Java平台所定义的五种字体系列：Serif、SansSerif、Monospaced、Dialog
                    // 和
                    // DialogInput
                    Font font = new Font("新宋体", Font.BOLD, 10);
                    Font font2 = new Font("新宋体", Font.BOLD, 8);
                    g2_1.setFont(font2); // 设置字体
                    // BasicStroke bs_3=new
                    // BasicStroke(0.5f);
                    float[] dash1 = { 2.0f };
                    // 设置打印线的属性。
                    // 1.线宽
                    // 2、3、不知道，4、空白的宽度，5、虚线的宽度，6、偏移量
                    g2_1.setStroke(new BasicStroke(0.5f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER, 2.0f, dash1, 0.0f));
                    double x = pf.getImageableX();
                    double y = pf.getImageableY();
                    float heigth1 = font.getSize2D();
                    float heigth = font2.getSize2D() + 2; // 字体高度
                    PrinterMessage bean1 = list.get(0);
                    // 打印菜单头部信息
                    g2_1.setFont(font); // 设置字体
                    g2_1.drawString(bean1.getBrand(), (float) x + 10, (float) y + 1 * heigth1);
                    g2_1.drawString(bean1.getShop(), (float) x + 20, (int) (y + 2 * heigth1));
                    g2_1.drawString("", (float) x + 20, (int) (y + 3 * heigth1));
                    g2_1.setFont(font2); // 设置字体
                    g2_1.drawString("现金：" + bean1.getCash(), (float) x, (float) y + 4 * heigth1);
                    g2_1.drawString("银联卡：" + bean1.getCup(), (float) x + 50, (float) y + 4 * heigth1);
                    g2_1.drawString("单号：" + bean1.getOrderNo(), (float) x, (float) y + 5 * heigth1);
                    g2_1.drawString("台号：" + bean1.getTableName(), (float) x + 50, (float) y + 5 * heigth1);
                    g2_1.drawString("人数：" + bean1.getPerNum(), (float) x, (float) y + 6 * heigth1);
                    g2_1.setFont(font2);
                    g2_1.drawString("菜式", (float) x, (float) y + 7 * heigth1);
                    g2_1.setFont(font2); // 设置字体
                    g2_1.drawString("数量", (float) x + 90, (float) y + 7 * heigth1);
                    g2_1.setFont(font2); // 设置字体
                    g2_1.drawString("单价", (float) x + 115, (float) y + 7 * heigth1);
                    // 曲线
                    // g2_1.drawLine((int) x - 2,
                    // (int) (y + 5 * (heigth -
                    // 2)), (int) x + 183, (int)
                    // (y + 5 * (heigth - 2)));
                    int y1 = 0;
                    g2_1.setFont(font2);
                    // 打印菜单内容
                    for (PrinterMessage bean : list) {
                        g2_1.drawString(bean.getItemName(), (float) x, (float) y + ((float) 8 + y1) * heigth);
                        g2_1.drawString(bean.getQty() + "", (float) x + 95, (float) y + ((float) 8 + y1) * heigth);
                        g2_1.drawString(bean.getPrice() + "", (float) x + 110, (float) y + ((float) 8 + y1) * heigth);
                        y1++;
                    }
                    // 打印菜单尾部信息
                    g2_1.drawString("", (float) x + 20, (float) y + ((float) 8 + y1) * heigth);
                    y1++;
                    g2_1.drawString("茶位：" + bean1.getCash(), (float) x + 10, (float) y + ((float) 8 + y1) * heigth);
                    y1++;
                    g2_1.drawString("总价：" + bean1.getTotalPrice(), (float) x + 10, (float) y + ((float) 8 + y1) * heigth);
                    y1++;
                    g2_1.drawString("实收：" + bean1.getPaidIn(), (float) x + 10, (float) y + ((float) 8 + y1) * heigth);
                    y1++;
                    g2_1.drawString("日期：" + new Date() + " ##", (float) x, (float) y + ((float) 8 + y1) * heigth);
                    y1++;
                    g2_1.drawString("", (float) x + 20, (float) y + ((float) 8 + y1) * heigth);
                    y1++;
                    g2_1.drawString("", (float) x + 20, (float) y + ((float) 8 + y1) * heigth);
                    y1++;
                    g2_1.drawString("", (float) x + 20, (float) y + ((float) 8 + y1) * heigth);
                    y1++;
                    g2_1.drawString("", (float) x + 20, (float) y + ((float) 8 + y1) * heigth);
                    return PAGE_EXISTS;
                default:
                    return NO_SUCH_PAGE;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }
}